<?php
/**
 * 字段类型为array类型
 * Enter description here ...
 * @author nijn
 *
 */
class ArrayAttributeBehavior extends CActiveRecordBehavior{

	/**
	 * 映射的字段
	 * Enter description here ...
	 * @var unknown_type
	 */
	public $attributes;
	public $glue = ',';
	
	public function beforeValidate() {
		$owner = $this->getOwner();
		foreach ($this->attributes as $oneAttr) {
			$owner->{$oneAttr} = 
				is_array($owner->{$oneAttr}) ? 
				join($this->glue, $owner->{$oneAttr}) : 
				$owner->{$oneAttr};
			$owner->{$oneAttr} = str_ireplace($this->glue.$this->glue, $this->glue, $owner->{$oneAttr});
			$owner->{$oneAttr} = rtrim($owner->{$oneAttr}, $this->glue);
		}
	}
	
	
	public function afterFind() {
		$owner = $this->getOwner();
		foreach ($this->attributes as $oneAttr) {
			$owner->{$oneAttr} = 
				empty($owner->{$oneAttr})? 
				array() : 
				is_array($owner->{$oneAttr}) ? $owner->{$oneAttr} : explode($this->glue, $owner->{$oneAttr});
		}
	}
}

?>